package com.techsen.maven.plugins.util;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.dom4j.Node;
import org.dom4j.XPath;

public class PomHelper extends XmlHelper {
    
    public PomHelper(File file) {
        super(file);
    }
    
    public PomHelper setVersion(String version) {
        XPath xpath = this.createXpath("/project:project/project:version");
        Node node = xpath.selectSingleNode(this.getDocument());
        if (node != null) {
            node.setText(version);
        }
        return this;
    }
    
    public PomHelper setParentVersion(String version) {
        XPath xpath = this.createXpath("/project:project/project:parent/project:version");
        Node node = xpath.selectSingleNode(this.getDocument());
        if (node != null) {
            node.setText(version);
        }
        return this;
    }
    
    @SuppressWarnings("unchecked")
    public List<String> getModules() {
        List<String> modules = new ArrayList<String>();
        XPath xpath = this.createXpath("/project:project/project:modules/project:module");
        List<Node> list = xpath.selectNodes(this.getDocument());
        if (list != null && list.size() > 0) {
            for (Node node : list) {
                modules.add(node.getText().trim());
            }
        }
        return modules;
    }
    
}
